<?php

include 'ConnectDB.php';
include 'graph.php';
define('FPDF_FONTPATH', './fpdf/font/');
require_once ("fpdf/fpdf.php");
$connect = new ConnectDB();
$connect->connect();

Class BMS {

    function gettotalsell($connect) {

        $query = "SELECT distinct brand_c FROM sellout_products  
            INNER JOIN sell_sellout_cstm ON sell_out = id_c WHERE YEAR(date_s_c) = YEAR(NOW())";
        $result = $connect->getdata($query);
        return $result;
    }

    function totalbymotnth($connect, $brands) {
        $query = "select brand_c, sum(total_price) as brand_total ,MONTH(date_s_c)
                    from sellout_products  
                    INNER JOIN sell_sellout_cstm ON sell_out = id_c WHERE YEAR(date_s_c) = YEAR(NOW())
                    group by brand_c , MONTH(date_s_c)";
        $insale = $connect->getdata($query);
        return $insale;
    }

    function ttbymonth($connect){
        $query = "select brand_c, sum(total_price) as brand_total ,MONTH(date_s_c)
                    from sellout_products  
                    INNER JOIN sell_sellout_cstm ON sell_out = id_c WHERE YEAR(date_s_c) = YEAR(NOW())
                    group by  MONTH(date_s_c)";
        $insale = $connect->getdata($query);
        return $insale;
    }
    function gettotalbybrand($connect) {
        $query = "select brand_c, sum(total_price) as brand_total
                    from sellout_products  
                    INNER JOIN sell_sellout_cstm ON sell_out = id_c WHERE YEAR(date_s_c) = YEAR(NOW())
                    group by brand_c";
        $total = $connect->getdata($query);
        return $total;
    }
    function gettotalall($connect) {

        $query = "select sum(total_price) as brand_total
                    from sellout_products  
                    INNER JOIN sell_sellout_cstm ON sell_out = id_c WHERE YEAR(date_s_c) = YEAR(NOW())";
        $result = $connect->getdata($query);
        return $result;
    }
}

Class PDF extends FPDF {

    function table($mtotal, $ttbybrand, $ttmonth,$month,$all) {
        $head = array("BRAND", "JAN", "FEB", "MRA", "APR", "MAR",
            "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC", "TOTAL");
        $ss = array(25, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 20);
        for ($i = 0; $i < count($head); $i++)
            $this->Cell($ss[$i], 5, iconv('UTF-8', 'TIS-620', $head[$i]), 1, 0, 'C'); //หัววคอลัมท่ี2
        $this->Ln();
        foreach ($ttbybrand as $ttbybrand) {
            $this->Cell(25, 6, iconv('UTF-8', 'TIS-620', $ttbybrand["brand_c"]), 1, 0, 'C');
            foreach ($ttmonth as $ttmonths) {
                if (strcasecmp($ttbybrand['brand_c'], $ttmonths['brand_c']) == 0) {
                    $this->Cell(18, 6, iconv('UTF-8', 'TIS-620',  number_format($ttmonths["brand_total"], 2, '.', '')), 1, 0, 'C');
                }
               
            }
             $this->Cell(20, 6, iconv('UTF-8', 'TIS-620',number_format($ttbybrand["brand_total"], 2, '.', '')), 1, 0, 'C');
            $this->Ln();
        }
        $this->Cell(25, 6, iconv('UTF-8', 'TIS-620', 'Total'), 1, 0, 'C');
        foreach($month as $months){
        $this->Cell(18, 6, iconv('UTF-8', 'TIS-620', number_format($months["brand_total"], 2, '.', '')), 1, 0, 'C');
        }
       
        $this->Cell(20, 6, iconv('UTF-8', 'TIS-620', number_format($all[0]['brand_total'], 2, '.', '')), 1, 0, 'C');
    }

}

$graph = new Reportgraph;
$pdf = new PDF;
$bms = new BMS;
$mtotal = $bms->gettotalsell($connect);
$ttmonth = $bms->totalbymotnth($connect, $mtotal);
$ttbybrand = $bms->gettotalbybrand($connect);
$month = $bms->ttbymonth($connect);
$all = $bms->gettotalall($connect);
$graph->linegraph($ttmonth,$ttbybrand);
$pdf->AddFont('tahoma', '', 'tahoma.php');
$pdf->SetFont('tahoma', '', 11);
$pdf->AddPage('L');
$pdf->Image('bms.png', 50, 50, 200,100);
$pdf->table($mtotal, $ttbybrand, $ttmonth,$month,$all);
$pdf->Output('pdf/bms.pdf');
?>